// ==UserScript==
// @icon         https://xxxclub.to/assets/icons/favicon.ico
// @name         RARBG-ASSIST
// @namespace    https://rarbgprx.org/
// @version      1.2.3
// @description  RARBG-ASSIST
// @author       MenShiyun
// @match        *://rarbgprx.org/*
// @match        *://rarbgproxied.org/*
// @match        *://rarbgmirror.com/*
// @match        *://rarbgmirror.org/*
// @match        *://rarbgaccess.org/*
// @match        *://proxyrarbg.org/*
// @match        *://xxxclub.to/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    var lista2 = document.getElementsByClassName('lista2');
    if (lista2.length > 0) {
        var footer = document.querySelector('body > div:nth-child(7)');
        var footline = footer.innerText.split('\n');
        var webtime = footline[footline.length - 1];
        var timezone = webtime.match(/\+\S+/)[0];
        var out_time = new Date(webtime) - 24 * 60 * 60 * 1000;

        for (var i = 0; i < lista2.length; i++) {
            var row = lista2[i];
            var col_cat = row.cells[0];
            var col_file = row.cells[1];
            var col_time = row.cells[2];

            col_file.children[0].setAttribute('target', '_blank');

            if ('onmouseover' in col_file.children[0].attributes) {
                var preview = col_file.children[0].attributes.onmouseover.value;
                preview = preview.match(/\\'\S+\\'/)[0];
                preview = preview.replace(/\\'/g, '');
                col_cat.children[0].children[0].src = preview;
            }

            if (new Date(col_time.innerText + timezone) > out_time) {
                row.setAttribute('style', 'background: #d3dbe9');
            }
        }
    }

    var table_classes = ["browsetableinside", "divtableinside", "similarinside"];
    for (let c of table_classes) {
        var browsetableinside = document.getElementsByClassName(c);
        if (browsetableinside.length > 0) {
            for (let l of browsetableinside) {
                var lista2 = l.children[0].children;
                if (lista2.length > 0) {
                    for (var i = 1; i < lista2.length; i++) {
                        var row = lista2[i];
                        var col_cat = row.children[0];
                        var col_file = row.children[1];

                        col_file.children[0].setAttribute('target', '_blank');

                        var img = new Image();
                        img.src = col_file.children[col_file.children.length - 1].attributes.src.value;
                        img.style.display = "unset";

                        col_cat.appendChild(img);
                    }
                }
            }
        }
    }

    var fix_img_style = false;

    var desc = document.getElementById('description');
    if (desc == null) {
        desc = document.getElementsByClassName('description');
        if (desc.length > 0) {
            desc = desc[0];
            fix_img_style = true;
        } else {
            desc = null;
        }
    }

    if (desc != null) {
        for (var n = 0; n < desc.children.length; n++) {
            var desc_child = desc.children[n];
            if (desc_child.tagName != 'A') {
                continue;
            }

            var image = desc_child.href;

            if (/(22pixx\S+\/\w+-\w+\/\S+\.html|imgtraffic)/.test(image)) {
                image = image.replace(/\/\w+-/, '/');
                image = image.replace(/\.html/, '');
            } else if (/imagecurl\S+\/viewer.php\?file=/.test(image)) {
                image = image.replace(/imagecurl\./, 'cdn.imagecurl.');
                image = image.replace(/\/viewer\.php\?file=/, '/images/');
            } else if (/(freebunker|imagefruit|imagesnake|imgshots|pornbus|imgcarry)\S+\/show\//.test(image)) {
                image = image.replace(/\/show\//, '/tn/i');
            } else {
                continue;
            }

            desc_child.href = image;
            desc_child.children[0].src = image;

            if (fix_img_style) {
                desc_child.children[0].classList = null;
                desc_child.children[0].style = "max-width: unset!important;";
            }
        }
    }
})();
